package top.yudoge.dogeeditorserverj.web.controller;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.yudoge.dogeeditorserverj.common.objects.Response;
import top.yudoge.dogeeditorserverj.db.config.SQLDataSource;
import top.yudoge.dogeeditorserverj.service.DataBaseMetaDataService;
import top.yudoge.dogeeditorserverj.transients.vo.SQLDataSourceVO;

import java.util.List;

@RestController
@RequestMapping("/db-metadata")
public class DataBaseMetaDataController {

    private DataBaseMetaDataService dataBaseMetaDataService;

    public DataBaseMetaDataController(DataBaseMetaDataService dataBaseMetaDataService) {
        this.dataBaseMetaDataService = dataBaseMetaDataService;
    }

    /**
     * 获取所有数据源
     * @return
     */
    @GetMapping
    public Response<List<SQLDataSourceVO>> getDataSources() {
        return Response.ok(BeanUtil.copyToList(dataBaseMetaDataService.getDataSources(), SQLDataSourceVO.class));
    }

    /**
     * 获取数据源下的所有数据库
     * @param dsName
     * @return
     */
    @GetMapping("/{dsName}")
    public Response<List<String>> getDatabases(@PathVariable("dsName") String dsName) {
        return Response.ok(dataBaseMetaDataService.getDatabase(dsName));
    }

    /**
     * 获取数据库下的所有schema
     * @param dsName
     * @param dbName
     * @return
     */
    @GetMapping("/{dsName}/{dbName}")
    public Response<List<String>> getSchema(@PathVariable("dsName") String dsName, @PathVariable("dbName") String dbName) {
        return Response.ok(dataBaseMetaDataService.getSchema(dsName, dbName));
    }

    /**
     * 获取schema下的所有表
     * @param dsName
     * @param dbName
     * @param schemaName
     * @return
     */
    @GetMapping("/{dsName}/{dbName}/{schemaName}")
    public Response<List<String>> getTable(@PathVariable("dsName") String dsName, @PathVariable("dbName") String dbName, @PathVariable("schemaName") String schemaName) {
        return Response.ok(dataBaseMetaDataService.getTable(dsName, dbName, schemaName).stream().map(it -> it.getTableName()).toList());
    }

    /**
     * 刷新数据源下的数据库列表
     * @return
     */
    @GetMapping("/refresh/{dsName}")
    public Response<Void> refresh(@PathVariable("dsName") String dsName) {
        dataBaseMetaDataService.refresh(dsName);
        return Response.ok(null);
    }

    /**
     * 刷新数据库下的schema列表
     * @return
     */
    @GetMapping("/refresh/{dsName}/{dbName}")
    public Response<Void> refresh(
            @PathVariable("dsName") String dsName,
            @PathVariable("dbName") String dbName
    ) {
        dataBaseMetaDataService.refresh(dsName, dbName);
        return Response.ok(null);
    }

    /**
     * 刷新schema下的表列表
     * @return
     */
    @GetMapping("/refresh/{dsName}/{dbName}/{schema}")
    public Response<Void> refresh(
            @PathVariable("dsName") String dsName,
            @PathVariable("dbName") String dbName,
            @PathVariable("schema") String schema
    ) {
        dataBaseMetaDataService.refresh(dsName, dbName, schema);
        return Response.ok(null);
    }

    /**
     * 表结构刷新
     * @return
     */
    @GetMapping("/refresh/{dsName}/{dbName}/{schema}/{tableName}")
    public Response<Void> refresh(
            @PathVariable("dsName") String dsName,
            @PathVariable("dbName") String dbName,
            @PathVariable("schema") String schema,
            @PathVariable("tableName") String tableName
    ) {
        dataBaseMetaDataService.refresh(dsName, dbName, schema, tableName);
        return Response.ok(null);
    }

}
